/*
 * Copyright (C) 2017 Honeywell, Inc. All Rights Reserved.
 */
package org.thanos.demo.consumer;

import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class DemoConsumer {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoConsumer.class);

    @Autowired
    private Connection connection;

    @Value("${spring.nats.topic}")
    private String topic;

    public void consume() {
        Dispatcher dispatcher = connection.createDispatcher(message -> LOGGER.info(new String(message.getData(), StandardCharsets.UTF_8)));
        dispatcher.subscribe(topic);
    }
}
